# -*- coding: utf-8 -*-
"""
Created on Sat Jun  9 12:46:29 2012

@author: nzhao
"""

from traits.api import HasTraits, Instance, DelegatesTo
from traitsui.api import View, Group, Item, TableEditor
from traitsui.table_column import ObjectColumn, ExpressionColumn
from traitsui.table_filter import EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate, EvalTableFilter

import numpy as np

from core.SingleSpin import SingleSpin
from core.MultiSpin import MultiSpin


table_editor = TableEditor(
    columns = [ ObjectColumn( name = 'spin_element', width = 0.20 ),
                ObjectColumn( name = 'x',  width = 0.20 ),
                ObjectColumn( name = 'y',  width = 0.20 ),
                ObjectColumn( name = 'z',  width = 0.20 ),
                ObjectColumn( name = 'spin_type',  width = 0.20 ),
                ExpressionColumn(
                    label = 'Distance',
                    width = 0.20,
                    expression = "'%f' % ( ( object.x**2 + object.y**2 + object.z**2)**0.5) " )
                ],
    deletable   = True,
    sort_model  = True,
    auto_size   = False,
    orientation = 'vertical',
    filters     = [ EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate ],
    search      = EvalTableFilter(),
    show_toolbar = True,
    row_factory = SingleSpin)

spin_viewer_group = Group(
            Item( 'spin_list',
                  show_label  = False,
                  editor      = table_editor
            ),
            show_border = True,
            label = 'Spin Viewer'
        )
spin_viewer_view = View(
        spin_viewer_group,
        title     = 'Spin Viewer',
        width     = .4,
        height    = .4,
        resizable = True,
        buttons   = [ 'OK' ],
        kind      = 'live'
    )
class SpinViewer(HasTraits):
    spin_list = DelegatesTo('multi_spin')
    multi_spin = Instance(MultiSpin)
    


if __name__ == '__main__':
    sv=SpinViewer(multi_spin = MultiSpin() )
    sv.configure_traits(view=spin_viewer_view)